package com.chengqj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.SessionSynchronization;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

/**
 * Copyright © 2020年 com.qxkj. All rights reserved.
 *
 * @author chengqj
 * TODO: 为了共建和谐社会，请为该类写点注释吧...
 * version 1.0.0 (接口必须带上)
 * @date 2020/11/11 16:53
 */
@Component
public class MongoUtils {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 与任何事务（即使是空事务）同步，并在执行此操作时启动MongoDB事务
     */
    public void setSessionSynchronizationForTransactionBegin() {

        mongoTemplate.setSessionSynchronization(SessionSynchronization.ALWAYS);

    }

    /**
     * 与通过启动的本机MongoDB事务同步
     */
    public void setSessionSynchronizationForTransactionCompletion() {

        mongoTemplate.setSessionSynchronization(SessionSynchronization.ON_ACTUAL_TRANSACTION);

    }

}
